package com.shujia.wyh.day14.tcpdemo2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/*
    接收客户端的数据文件，保存到E:\shujia\class23
 */
public class ServerDemo {
    public static void main(String[] args) throws Exception {
        //创建服务器端的Socket对象
        ServerSocket ss = new ServerSocket(10086);

        //监听客户端的连接
        Socket socket = ss.accept();
        String hostName = socket.getInetAddress().getHostName();
        long time = System.currentTimeMillis();
        String name = hostName + "-" + time;

        //创建本地的输出流
        BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\shujia\\class23\\" + name + ".txt"));

        //获取通道中的输入流对象
        InputStream inputStream = socket.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader br = new BufferedReader(inputStreamReader);

        String line = null;
        while ((line = br.readLine()) != null) {
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        //给客户端一个反馈
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("文件已经上传成功!!".getBytes());
        outputStream.flush();


        //关闭与客户端的连接
        socket.close();

    }
}
